package cn.newgrand.netcall.integration.sms;

import java.util.Collection;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.spark.SparkManager;

import cn.newgrand.netcall.common.StringUtilCommon;

public class ContactsPresenceListener implements RosterListener{
	
	public ContactsPresenceListener(){
		Roster roster = SparkManager.getConnection().getRoster();
		Collection<RosterEntry> enterys = roster.getEntries();
		for (RosterEntry rosterEntry : enterys) {
			String username = StringUtilCommon.parseName(rosterEntry.getUser());
			Presence p = roster.getPresence(rosterEntry.getUser());
			if(Presence.Mode.xa == p.getMode()){
				SMSManager.awayDateMap.put(username, System.currentTimeMillis());
				//System.out.println(username+"---"+System.currentTimeMillis());
			}
		}
	}
	

	@Override
	public void entriesAdded(Collection<String> addresses) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void entriesUpdated(Collection<String> addresses) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void entriesDeleted(Collection<String> addresses) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void presenceChanged(Presence presence) {
		if(Presence.Mode.xa == presence.getMode()){
			String username = StringUtilCommon.parseName(presence.getFrom());
			SMSManager.awayDateMap.put(username, System.currentTimeMillis());
			//System.out.println(username+"---"+System.currentTimeMillis());
		}else{
			String username = StringUtilCommon.parseName(presence.getFrom());
			if(SMSManager.awayDateMap.containsKey(username)){
				SMSManager.awayDateMap.remove(username);
			}
		}
	}

}
